<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      <!-------------------------- 修改用戶 對話框 -->
    </el-breadcrumb>
    <el-dialog title="修改用戶" :visible.sync="editUserDialog" width="50%">
      <!----------------------------- 對話框內容 -->
      <el-form ref="editUserRef" :model="editUser" :rules="editUserRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="editUser.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="editUser.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editUser.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editUserDialog = false">取 消</el-button>

        <el-button type="primary" @click="xiugai()">确 定</el-button>
      </span>
    </el-dialog>
    <!-------------------------- 添加用戶 對話框 -->
    <el-dialog title="添加用戶" @close="resetFrom()" :visible.sync="addUserDialog" width="50%">
      <!----------------------------- 對話框內容 -->
      <el-form ref="addUserRef" :model="adduser" :rules="addUserRules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="adduser.username"></el-input>
          <!-- v-model="form.name" -->
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="adduser.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="adduser.mobile"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="adduser.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addUserDialog = false">取 消</el-button>
        <el-button type="primary" @click="tianjia()">添 加</el-button>
      </span>
    </el-dialog>
    <!--------------------------------- 卡片 -->
    <el-card class="box-card">
      <!------------------------------ input框 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            class="input-with-select"
            v-model="querycdt.query"
            @clear="search()"
            clearable
            @keyup.enter.native="search()"
          >
            <el-button slot="append" icon="el-icon-search" @click="search()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addUserDialog=true">添加用户</el-button>
        </el-col>
      </el-row>
      <!------------------- table表格 -->
      <el-table :data="userList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="username" label="用户名" width="250"></el-table-column>
        <el-table-column prop="mobile" label="手机号码" width="120"></el-table-column>
        <el-table-column prop="role_name" label="角色" width="180"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column prop="mg_state" label="状态" width="80">
          <el-switch v-model="info.row.mg_state" slot-scope="info"></el-switch>
        </el-table-column>
        <!-- 提示信息 -->
        <el-table-column label="操作">
          <template slot-scope="info">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showEditDialog(info.row.id)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="delUser(info.row.id)"
            ></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button type="warning" size="mini" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--------------------- 分頁功能 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="querycdt.pagenum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="2"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tot"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getUserList()
  },
  data() {
    // 给手机号码添加自定义验证规则
    var checkmobile = (rule, value, callback) => {
      var reg = /^1[3456789]\d{9}$/
      if (!reg.test(value)) {
        // 验证失败
        return callback(new Error('手机格式输入不正确'))
      }
      callback()
    }
    return {
      // 修改---------给表单域做验证
      editUserRules: {
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' },
          { validator: checkmobile, trigger: 'blur' } // 应用自定义规则
        ]
      },
      // 表单数据对象
      editUser: {
        username: '',
        email: '',
        mobile: ''
      },
      // 添加---------给表单域做验证
      addUserRules: {
        username: [{ required: true, message: '用户名必填', trigger: 'blur' }],
        password: [{ required: true, message: '密码必填', trigger: 'blur' }],
        mobile: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { validator: checkmobile, trigger: 'blur' } // 应用自定义规则
        ]
      },
      // 表单数据对象
      adduser: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      tot: 0,
      userList: [],
      addUserDialog: false,
      editUserDialog: false,
      querycdt: {
        query: '', // 搜索关键字
        pagenum: 1, // 当前页码
        pagesize: 2 // 每页显示数据条数
      }
    }
  },
  methods: {
    // 修改功能实现
    xiugai() {
      this.$refs.editUserRef.validate(async valid => {
        if (valid) {
          const { data: dt } = await this.$http.put(
            'users/' + this.editUser.id,
            this.editUser
          )
          if (dt.meta.status !== 200) {
            return this.$message.error(dt.meta.msg)
          }
          this.$message.success(dt.meta.msg)
          this.editUserDialog = false
          this.getUserList()
          // console.log(dt)
        }
      })
    },
    // 点击编辑按钮后弹出form表单及数据回显
    async showEditDialog(id) {
      this.editUserDialog = true
      const { data: dt } = await this.$http.get('users/' + id)
      // console.log(dt)
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }
      this.editUser = dt.data
    },
    // 删除用户
    delUser(id) {
      this.$confirm('确定要删除该用户么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { data: dt } = await this.$http.delete('users/' + id)
          console.log(dt)
          if (dt.meta.status !== 200) {
            return this.$message.error(dt.meta.msg)
          }
          if (this.userList.length === 1 && this.querycdt.pagenum > 1) {
            this.querycdt.pagenum--
          }
          this.getUserList()
        })
        .catch(() => {})
    },
    // 添加用户
    tianjia() {
      this.$refs.addUserRef.validate(async valid => {
        if (valid) {
          const { data: dt } = await this.$http.post('users', this.adduser)
          // console.log(dt)
          if (dt.meta.status !== 201) {
            return this.$message.error(dt.meta.msg)
          }
          this.addUserDialog = false
          this.$message.success(dt.meta.msg)
          this.getUserList()
        }
      })
    },
    resetFrom() {
      this.$refs.addUserRef.resetFields()
    },
    handleSizeChange(val) {
      this.querycdt.pagesize = val
      this.getUserList()
    },
    handleCurrentChange(val) {
      this.querycdt.pagenum = val
      this.getUserList()
    },
    async getUserList() {
      const { data: dt } = await this.$http.get('/users', {
        params: this.querycdt
      })
      // console.log(dt)
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }
      this.tot = dt.data.total
      this.userList = dt.data.users
    },
    search() {
      this.getUserList()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
